package indi.kider.server.bill.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.bill.*;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.meter.CommandResponse;
import indi.kider.server.bill.service.MeterService;
import indi.kider.server.bill.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;

/**
 * Created by shixj on 2018/5/28.
 */
@RestController
@RequestMapping("meter")
public class MeterController {
    @Autowired
    private MeterService meterService;
    @Autowired
    private AuthcService authcService;
    /**
     * @api {PUT} meter/command
     * @apiGroup balance-bill
     * @apiDescription 拉合闸  meterType:1水2电  opType;//0合闸1拉闸
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter/command
     * {"housesId":1,"meterType":1,"opType":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"操作成功！","data":null}}
     * {"status":"400","msg":"命令执行失败！","data":null}}
     */
    @PutMapping("command")
    public CommonResponse<CommandResponse> meterControl(@RequestBody MeterControl meterControl) {
        try {
            return meterService.meterControl(meterControl);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {PUT} meter/name
     * @apiGroup balance-bill
     * @apiDescription 修改表名称
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter/name
     * {"id":1,"name":"谁的表"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":null}}
     */
    @PutMapping("name")
    public CommonResponse update(@RequestBody MeterNameChange meterNameChange) {
        try {
            meterService.update(meterNameChange);
            return CommonResponse.buildUpdateSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} meter/list
     * @apiGroup balance-bill
     * @apiDescription 查询表具列表 expression支持houseNumb，address，tenantTel，tenantName模糊匹配
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter/list
     * {"size":999,"num":1,"query":{"expression":1,"rentId":1,"housesId":"1"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"housesId":69,"meterCode":"110","meterType":1,"locationParam":1,"concentrator":"1","name":"谁的表","rentId":87,"tenantName":"1","tenantTel":"1","houseNumb":"WN_0000","building":null,"unit":null,"address":"蒋村","community":"都市水乡","communityId":null,"expression":null,"enterpriseId":null}],"total":1,"size":1}}
     */
    @PostMapping("list")
    public CommonResponse<PageResponse<List<RentMeter>>> getRentMeterList(@RequestBody PageRequest<RentMeter> request) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            if(employee!=null&&!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//超级管理员
                request.getQuery().setEnterpriseId(employee.getEnterpriseId());
            }
            PageUtil.start(request);
            return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse( meterService.getRentMeterList(request.getQuery())));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} meter/record/list
     * @apiGroup balance-bill
     * @apiDescription 查询读数列表  meterType：1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter/record/list
     * {"size":999,"num":1,"query":{"rentId":1,"meterType":1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"title":"2018年05月份","thisRead":3.0,"thisReadTime":"2018-05-28 09:54","lastRead":0.0,"lastReadTime":null,"absoluteNumber":3.0},{"title":"2018年05月份","thisRead":3.0,"thisReadTime":"2018-05-28 09:54","lastRead":1.0,"lastReadTime":null,"absoluteNumber":2.0}],"total":2,"size":1}}
     */
    @PostMapping("record/list")
    public CommonResponse<PageResponse<List<MeterRecord>>> getMeterRecordList(@RequestBody PageRequest<RentMeter> request) {
            PageUtil.start(request);
            return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse( meterService.getMeterRecordList(request.getQuery())));
    }

    /**
     * @api {POST} meter
     * @apiGroup balance-bill
     * @apiDescription 绑定表具  meterType：1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter
     * {"housesId":1,"meterType":1,"meterCode":"123123123"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":""}
     */
    @PostMapping("")
    public CommonResponse<Meter> bindHouseMeter(@RequestBody Meter meter) {
        try {
            meterService.bindHouseMeter(meter);
            return CommonResponse.buildAddSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {DELETE} meter
     * @apiGroup balance-bill
     * @apiDescription 解除绑定表具  meterType：1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/meter/1/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}
     */
    @DeleteMapping ("/{meterId}/{housesId}")
    public CommonResponse<Meter> cleanHouseMeter(@PathVariable Long meterId,@PathVariable Long housesId) {
        try {
            meterService.cleanHouseMeter(meterId,housesId);
            return CommonResponse.buildDelSuccessResponse("解绑成功！","");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {POST} meter
     * @apiGroup balance-bill
     * @apiDescription 查询表具  meterType：1水2电
     * @apiParamExample 请求样例：
     * http://localhost:9002/balance-bill/selectOneMeter
     * {"housesId":1,"meterType":1}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":""}
     */
    @PostMapping("selectOneMeter")
    public CommonResponse<Meter> selectOneMeter(@RequestBody Meter meter) {
        try {
            return CommonResponse.buildGetSuccessResponse(meterService.selectOne(meter));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }

    }
}
